﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;
using DataAccess;
using Common;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class PhieuTTKH : MarshalByRefObject
    {
        static void LuuViet(string hanhDong, string maNhanVien, string noiDung, string tenDangNhap, string thoiGianThucHien)
        {
            try
            {
                Entities.LuuVietHeThong lv = new Entities.LuuVietHeThong();
                lv.HanhDong = hanhDong;
                lv.MaNhanVien = maNhanVien;
                lv.NoiDung = noiDung;
                lv.TenDangNhap = tenDangNhap;
                lv.ThoiGianThucHien = thoiGianThucHien;
                new LuuVietHeThong().LuuViet(lv);
            }
            catch
            {
                return;
            }
        }

        private static SqlCommand cmd;
        private static SqlDataReader dr;
        private static SqlConnection cn;

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.PhieuTTCuaKH[] Select()
        {
            string sql =Common.Constants.PhieuTTCuaKH.SelectPhieuTTKH;
            
            cn = DataAccess.Connection.OpenConnection();
            cmd = new SqlCommand(sql, cn);
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            ArrayList arr = new ArrayList();
            while (dr.Read())
            {
                Entities.PhieuTTCuaKH row = new Entities.PhieuTTCuaKH();

                row.PhieuTTCuaKHID =int.Parse(dr[Common.Constants.PhieuTTCuaKH.PhieuTTCuaKHID].ToString());
                row.MaPhieuTTCuaKH = dr[Common.Constants.PhieuTTCuaKH.MaPhieuTTCuaKH].ToString();
                row.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuTTCuaKH.NgayLap].ToString());
                row.MaKhachHang = dr[Common.Constants.PhieuTTCuaKH.MaKhachHang].ToString();
                row.TenKhachHang = dr[Common.Constants.PhieuTTCuaKH.TenKhachHang].ToString();
                row.MaNhanVien = dr[Common.Constants.PhieuTTCuaKH.MaNhanVien].ToString();
                row.MaKho = dr[Common.Constants.PhieuTTCuaKH.MaKho].ToString();
                row.TongThanhToan = dr[Common.Constants.PhieuTTCuaKH.TongThanhToan].ToString();
                row.NoHienThoi = dr[Common.Constants.PhieuTTCuaKH.NoHienThoi].ToString();
                row.NguoiNop = dr[Common.Constants.PhieuTTCuaKH.NguoiNop].ToString();
                row.LoaiPhieu = bool.Parse(dr[Common.Constants.PhieuTTCuaKH.LoaiPhieu].ToString());
                row.GhiChu = dr[Common.Constants.PhieuTTCuaKH.GhiChu].ToString();
                row.Deleted = bool.Parse(dr[Common.Constants.PhieuTTCuaKH.Deleted].ToString());
                arr.Add(row);

            }
            int n = arr.Count;
            if (n == 0) return null;

            Entities.PhieuTTCuaKH[] arrC = new Entities.PhieuTTCuaKH[n];
            for (int i = 0; i < n; i++)
            {
                arrC[i] = (Entities.PhieuTTCuaKH)arr[i];
            }

            //Giai phong bo nho
            arr = null;
            cmd.Connection.Dispose();
            cn.Close();
            cn = null;
            
            return arrC;
      
        }

       /// <summary>
       /// select
       /// </summary>
       /// <param name="hdbh1"></param>
       /// <returns></returns>
        public static Entities.PhieuTTCuaKH[] SelectTheoDiaDiem(string MaKho)
       {
           string sql = Common.Constants.PhieuTTCuaKH.SelectPhieuTTKHTheoDiaDiem;
           
           cn = DataAccess.Connection.OpenConnection();
           cmd = new SqlCommand(sql, cn);
           cmd.Parameters.Add(Common.Constants.PhieuChi.MaKho, SqlDbType.VarChar, 50).Value = MaKho;
           dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

           ArrayList arr = new ArrayList();
           while (dr.Read())
           {
               Entities.PhieuTTCuaKH row = new Entities.PhieuTTCuaKH();

               row.PhieuTTCuaKHID = int.Parse(dr[Common.Constants.PhieuTTCuaKH.PhieuTTCuaKHID].ToString());
               row.MaPhieuTTCuaKH = dr[Common.Constants.PhieuTTCuaKH.MaPhieuTTCuaKH].ToString();
               row.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuTTCuaKH.NgayLap].ToString());
               row.MaKhachHang = dr[Common.Constants.PhieuTTCuaKH.MaKhachHang].ToString();
               row.TenKhachHang = dr[Common.Constants.PhieuTTCuaKH.TenKhachHang].ToString();
               row.MaNhanVien = dr[Common.Constants.PhieuTTCuaKH.MaNhanVien].ToString();
               row.MaKho = dr[Common.Constants.PhieuTTCuaKH.MaKho].ToString();
               row.TongThanhToan = dr[Common.Constants.PhieuTTCuaKH.TongThanhToan].ToString();
               row.NoHienThoi = dr[Common.Constants.PhieuTTCuaKH.NoHienThoi].ToString();
               row.NguoiNop = dr[Common.Constants.PhieuTTCuaKH.NguoiNop].ToString();
               row.LoaiPhieu = bool.Parse(dr[Common.Constants.PhieuTTCuaKH.LoaiPhieu].ToString());
               row.GhiChu = dr[Common.Constants.PhieuTTCuaKH.GhiChu].ToString();
               row.Deleted = bool.Parse(dr[Common.Constants.PhieuTTCuaKH.Deleted].ToString());
               arr.Add(row);

           }
           int n = arr.Count;
           if (n == 0) return null;

           Entities.PhieuTTCuaKH[] arrC = new Entities.PhieuTTCuaKH[n];
           for (int i = 0; i < n; i++)
           {
               arrC[i] = (Entities.PhieuTTCuaKH)arr[i];
           }

           //Giai phong bo nho
           arr = null;
           cmd.Connection.Dispose();
           cn.Close();
           cn = null;
           
           return arrC;

       }
      
        /// <summary>
        /// insert
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool InsertUpdate(Entities.PhieuTTCuaKH phieuthanhtoankhachhang)
        {
            TransactionScope ts = null;
           try
           {
               using (ts = new TransactionScope(TransactionScopeOption.Required))
               {
                   string sql = Common.Constants.PhieuTTCuaKH.InsertUpdatePhieuTTKH;
                   cn = DataAccess.Connection.OpenConnection();
                   cmd = new SqlCommand(sql, cn);

                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.HanhDong, SqlDbType.NVarChar, 20).Value = phieuthanhtoankhachhang.HanhDong;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.PhieuTTCuaKHID, SqlDbType.Int).Value = phieuthanhtoankhachhang.PhieuTTCuaKHID;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaPhieuTTCuaKH, SqlDbType.VarChar, 50).Value = phieuthanhtoankhachhang.MaPhieuTTCuaKH;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NgayLap, SqlDbType.DateTime).Value = phieuthanhtoankhachhang.NgayLap;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaKhachHang, SqlDbType.VarChar, 50).Value = phieuthanhtoankhachhang.MaKhachHang;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.TenKhachHang, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.TenKhachHang;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaNhanVien, SqlDbType.NVarChar, 50).Value = phieuthanhtoankhachhang.MaNhanVien;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaKho, SqlDbType.NVarChar, 50).Value = phieuthanhtoankhachhang.MaKho;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.TongThanhToan, SqlDbType.Float).Value = phieuthanhtoankhachhang.TongThanhToan;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NoHienThoi, SqlDbType.Float).Value = phieuthanhtoankhachhang.NoHienThoi;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NguoiNop, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.NguoiNop;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.LoaiPhieu, SqlDbType.Bit).Value = phieuthanhtoankhachhang.LoaiPhieu;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.GhiChu, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.GhiChu;
                   cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.Deleted, SqlDbType.Bit).Value = phieuthanhtoankhachhang.Deleted;
                   int i = cmd.ExecuteNonQuery();
                   bool kt = false;
                   if (i == 1)
                   {
                       kt = true;
                       if (phieuthanhtoankhachhang.HanhDong == Common.Constants.Insert)
                       {
                           LuuViet
                               (
                               "Thêm",
                               phieuthanhtoankhachhang.TaiKhoan.MaNhanVien,
                               "Thêm phiếu thanh toán khách hàng " + phieuthanhtoankhachhang.MaPhieuTTCuaKH,
                               phieuthanhtoankhachhang.TaiKhoan.TenDangNhap,
                               DateTime.Now.ToShortDateString()
                               );
                       }
                       else
                       {
                           LuuViet
                               (
                               "Sửa",
                               phieuthanhtoankhachhang.TaiKhoan.MaNhanVien,
                               "Sửa phiếu thanh toán khách hàng " + phieuthanhtoankhachhang.MaPhieuTTCuaKH,
                               phieuthanhtoankhachhang.TaiKhoan.TenDangNhap,
                               DateTime.Now.ToShortDateString()
                               );
                       }
                   }
                   else
                       kt = false;
                   cmd.Connection.Dispose();
                   cn.Close();
                   cn = null;

                   ts.Complete();
                   return kt;
               }
            }
           catch (Exception ex)
           {
               ts.Dispose();
               string s = ex.Message;
               return false;
           }
        }

        public static bool InsertUpdate(Entities.PhieuTTCuaKH[] phieu)
        {
            for (int i = 0; i < phieu.Length; i++)
            {
                InsertUpdate(phieu[i]);
            }
            return true;
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool Delete(Entities.PhieuTTCuaKH ptdel)
        {
            try
            {
                bool kt = false;
                string sql = Common.Constants.PhieuTTCuaKH.DeletePhieuTTKH;

                
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
          
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.HanhDong, SqlDbType.NVarChar, 20).Value = ptdel.HanhDong;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.PhieuTTCuaKHID, SqlDbType.Int).Value = ptdel.PhieuTTCuaKHID;
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    kt = true;


                    if (ptdel.HanhDong == Common.Constants.Insert)
                    {
                        LuuViet
                            (
                            "Xóa",
                            ptdel.TaiKhoan.MaNhanVien,
                            "Xóa phiếu thanh toán khách hàng " + ptdel.MaPhieuTTCuaKH,
                            ptdel.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToShortDateString()
                            );
                    }



                }
                else
                    kt = false;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return kt;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.HDBanBuon[] SelectHDBanBuon()
        {
            try
            {
                string sql = Common.Constants.PhieuTTCuaKH.SelectHDBanBuon;
                
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HDBanBuon row = new Entities.HDBanBuon();

                    row.ThanhToanNgay = (dr[Common.Constants.HDBanBuon.ThanhToanNgay].ToString());
                    row.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HDBanBuon.ThanhToanSauKhiLapPhieu].ToString();
                    row.TongTienThanhToan = (dr[Common.Constants.HDBanBuon.ThanhToanSauKhiLapPhieu].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.HDBanBuon[] arrC = new Entities.HDBanBuon[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.HDBanBuon)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch 
            {
                return new Entities.HDBanBuon[0];
            }
        }

        /// <summary>
        ///  update thanh toán sau khi lập phiếu vào hóa đơn bán buôn ( khi loại phiếu là Phiếu Thu )
        /// </summary>
        /// <param name="mahdbanbuon"></param>
        /// <param name="thanhtoansaulapphieu"></param>
        /// <returns></returns>
        public static bool InsertUpdate(string mahdbanbuon, string thanhtoansaulapphieu)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.PhieuTTCuaKH.Update;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.HDBanBuon.MaHDBanBuon, SqlDbType.VarChar, 50).Value = mahdbanbuon;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.ThanhToanSauKhiLapPhieu, SqlDbType.VarChar, 50).Value = thanhtoansaulapphieu;
                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        ///  update thanh toán sau khi lập phiếu vào khách hàng trả lại bán buôn ( khi loại phiếu là Phiếu Chi )
        /// </summary>
        /// <param name="mahdbanbuon"></param>
        /// <param name="thanhtoansaulapphieu"></param>
        /// <returns></returns>
        public static bool InsertUpdate(string makhtralai, string thanhtoansaulapphieu,string loai)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.PhieuTTCuaKH.UpdateKHTraLai;

                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaKhachHangTraLaiBanBuon, SqlDbType.VarChar, 50).Value = makhtralai;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.ThanhToanSauKhiLapPhieu, SqlDbType.VarChar, 50).Value = thanhtoansaulapphieu;
                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        ///  Update Dư Nợ Vào Khách Hàng
        /// </summary>
        /// <param name="mahdnhap"></param>
        /// <param name="thanhtoansaulapphieu"></param>
        /// <returns></returns>
        public static bool InsertUpdateDuNoKH(string makh, string duno)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.PhieuTTCuaKH.UpdateDuNoKH;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.KhachHang.MaKhachHang, SqlDbType.VarChar, 50).Value = makh;
                    cmd.Parameters.Add(Common.Constants.KhachHang.DuNo, SqlDbType.Float).Value = duno;
                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// loivt
        /// </summary>
        /// <param name="nhap"></param>
        /// <returns></returns>
        public static Entities.HoaDonNhap[] SelectTheoMaNCC(string mancc)
        {
            Entities.HoaDonNhap[] list = null;
            try
            {
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(Common.Constants.PhieuTTCuaKH.sp_Select_HoaDonNhapTheoMaNCC, cn);
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaNhaCungCap, SqlDbType.VarChar).Value = mancc;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();

                while (dr.Read())
                {
                    Entities.HoaDonNhap row = new Entities.HoaDonNhap();
                    row.MaHoaDonNhap = dr[Common.Constants.HoaDonNhap.MaHoaDonNhap].ToString();
                    row.NgayNhap = DateTime.Parse(dr[Common.Constants.HoaDonNhap.NgayNhap].ToString());
                    row.MaNhaCungCap = dr[Common.Constants.HoaDonNhap.MaNhaCungCap].ToString();
                    row.TenNhaCungCap = dr[Common.Constants.HoaDonNhap.TenNhaCungCap].ToString();
                    row.NoHienThoi = dr[Common.Constants.HoaDonNhap.NoHienThoi].ToString();
                    row.NguoiGiaoHang = dr[Common.Constants.HoaDonNhap.NguoiGiaoHang].ToString();
                    row.HinhThucThanhToan = dr[Common.Constants.HoaDonNhap.HinhThucThanhToan].ToString();
                    row.MaKho = dr[Common.Constants.HoaDonNhap.MaKho].ToString();
                    row.TenKho = dr[Common.Constants.HoaDonNhap.TenKho].ToString();
                    row.HanThanhToan = DateTime.Parse(dr[Common.Constants.HoaDonNhap.HanThanhToan].ToString());
                    row.MaDonDatHang = dr[Common.Constants.HoaDonNhap.MaDonDatHang].ToString();
                    row.MaTienTe = dr[Common.Constants.HoaDonNhap.MaTienTe].ToString();
                    row.TyGiaTienTe = dr[Common.Constants.HoaDonNhap.TyGiaTienTe].ToString();
                    row.ChietKhau = dr[Common.Constants.HoaDonNhap.ChietKhau].ToString();
                    row.HinhThucThanhToan = dr[Common.Constants.HoaDonNhap.HinhThucThanhToan].ToString();
                    row.ThanhToanNgay = dr[Common.Constants.HoaDonNhap.ThanhToanNgay].ToString();
                    row.ThueGTGT = dr[Common.Constants.HoaDonNhap.ThueGTGT].ToString();
                    row.TongTien = dr[Common.Constants.HoaDonNhap.TongTien].ToString();
                    row.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HoaDonNhap.ThanhToanSauKhiLapPhieu].ToString();
                    row.MaNhanVien = dr[Common.Constants.HoaDonNhap.MaNhanVien].ToString();
                    row.TenNhanVien = dr[Common.Constants.HoaDonNhap.TenNhanVien].ToString();
                    row.GhiChu = dr[Common.Constants.HoaDonNhap.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HoaDonNhap.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.HoaDonNhap[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.HoaDonNhap)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
            return list;
        }

        public static Entities.TraLaiNCC[] SelectHDTraLaiNCCTheoMaNCC(string mancc)
        {
            Entities.TraLaiNCC[] list = null;
            try
            {
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(Common.Constants.TraLaiNCC.SelectHDTraLaiNCCTheoMaNCC, cn);
                cmd.Parameters.Add(Common.Constants.TraLaiNCC.MaNCC, SqlDbType.VarChar).Value = mancc;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.TraLaiNCC TraLaiNCC = new Entities.TraLaiNCC();
                    TraLaiNCC.TraLaiNCCID = int.Parse(dr[Common.Constants.TraLaiNCC.TraLaiNCCID].ToString());
                    TraLaiNCC.MaHDTraLaiNCC = dr[Common.Constants.TraLaiNCC.MaHDTraLaiNCC].ToString();
                    TraLaiNCC.MaNhanVien = (dr[Common.Constants.TraLaiNCC.MaNhanVien].ToString());
                    TraLaiNCC.Ngaytra = DateTime.Parse(dr[Common.Constants.TraLaiNCC.Ngaytra].ToString());
                    TraLaiNCC.MaNCC = dr[Common.Constants.TraLaiNCC.MaNCC].ToString();
                    TraLaiNCC.TenNCC = dr[Common.Constants.TraLaiNCC.TenNCC].ToString();
                    TraLaiNCC.NoHienThoi = Common.Utilities.FormatMoney(dr[Common.Constants.TraLaiNCC.NoHienThoi].ToString());
                    TraLaiNCC.NguoiNhanHang = dr[Common.Constants.TraLaiNCC.NguoiNhanHang].ToString();
                    TraLaiNCC.MaKho = dr[Common.Constants.TraLaiNCC.MaKho].ToString();
                    TraLaiNCC.TenKho = dr[Common.Constants.TraLaiNCC.TenKho].ToString();
                    TraLaiNCC.TienBoiThuong = dr[Common.Constants.TraLaiNCC.TienBoiThuong].ToString();
                    TraLaiNCC.ThanhToanNgay = dr[Common.Constants.TraLaiNCC.ThanhToanNgay].ToString();
                    TraLaiNCC.ThanhToanSauKhiLapPhieu = dr[Common.Constants.TraLaiNCC.ThanhToanSauKhiLapPhieu].ToString();
                    TraLaiNCC.Deleted = bool.Parse(dr[Common.Constants.TraLaiNCC.Deleted].ToString());
                    TraLaiNCC.GhiChu = dr[Common.Constants.TraLaiNCC.GhiChu].ToString();
                    arr.Add(TraLaiNCC);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.TraLaiNCC[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.TraLaiNCC)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
            return list;
        }

        public static Entities.KhachHangTraLaiBanBuon[] SelectHDKHTraLaiBanBuonTheoMaKH(string makh)
        {
            Entities.KhachHangTraLaiBanBuon[] list = null;
            try
            {
                string sql = Common.Constants.KhachHangTraLaiBanBuon.SelectHDKHTraLaiBanBuonTheoMaKH;

                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaKhachHang, SqlDbType.VarChar, 50).Value = makh;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.KhachHangTraLaiBanBuon row = new Entities.KhachHangTraLaiBanBuon();
                    row.KhachHangTraLaiBanBuonID = int.Parse(dr[Common.Constants.KhachHangTraLaiBanBuon.KhachHangTraLaiBanBuonID].ToString());
                    row.MaKhachHangTraLaiBanBuon = dr[Common.Constants.KhachHangTraLaiBanBuon.MaKhachHangTraLaiBanBuon].ToString();
                    row.NgayNhap = DateTime.Parse(dr[Common.Constants.KhachHangTraLaiBanBuon.NgayNhap].ToString());
                    row.MaKhachHang = dr[Common.Constants.KhachHangTraLaiBanBuon.MaKhachHang].ToString();
                    row.TenKhachHang = dr[Common.Constants.KhachHangTraLaiBanBuon.TenKhachHang].ToString();
                    row.DuNo = dr[Common.Constants.KhachHangTraLaiBanBuon.DuNo].ToString();
                    row.NguoiTra = dr[Common.Constants.KhachHangTraLaiBanBuon.NguoiTra].ToString();
                    row.MaKho = dr[Common.Constants.KhachHangTraLaiBanBuon.MaKho].ToString();
                    row.TenKho = dr[Common.Constants.KhachHangTraLaiBanBuon.TenKho].ToString();
                    row.MaNhanVien = dr[Common.Constants.KhachHangTraLaiBanBuon.MaNhanVien].ToString();
                    row.TenNhanVien = dr[Common.Constants.KhachHangTraLaiBanBuon.TenNhanVien].ToString();
                    row.MaTienTe = dr[Common.Constants.KhachHangTraLaiBanBuon.MaTienTe].ToString();
                    row.TyGiaTienTe = dr[Common.Constants.KhachHangTraLaiBanBuon.TyGiaTienTe].ToString();
                    row.Thue = dr[Common.Constants.KhachHangTraLaiBanBuon.Thue].ToString();
                    row.TienBoiThuong = dr[Common.Constants.KhachHangTraLaiBanBuon.TienBoiThuong].ToString();
                    row.ThanhToanNgay = dr[Common.Constants.KhachHangTraLaiBanBuon.ThanhToanNgay].ToString();
                    row.ThanhToanSauKhiLapPhieu = dr[Common.Constants.KhachHangTraLaiBanBuon.ThanhToanSauKhiLapPhieu].ToString();
                    row.GhiChu = dr[Common.Constants.KhachHangTraLaiBanBuon.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.KhachHangTraLaiBanBuon.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.KhachHangTraLaiBanBuon[0];
                }
                else
                {
                    list = new Entities.KhachHangTraLaiBanBuon[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.KhachHangTraLaiBanBuon)arr[i];
                    }
                }
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.KhachHangTraLaiBanBuon[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
        }
    }
}

#endregion